package com.hx.service.Impl;

import com.hx.dto.UserDTO;
import com.hx.mapper.UserMapper;
import com.hx.pojo.User;
import com.hx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id查找用户信息
     * 返回UserDTO对象用于前端展示
     * @param userId
     * @return
     */
    @Override
    public UserDTO findUserById(String userId) {
        User user = userMapper.findUserById(userId);
        return convertPojoToDTO(user);
    }

    /**
     * pojo转化为DTO
     * @param user
     * @return
     */
    private UserDTO convertPojoToDTO(User user) {

        if (Objects.isNull(user)){
            log.error("user 信息查询为空！");
            return null;
        }

        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        userDTO.setName(user.getName());
        userDTO.setAccount(user.getAccount());
        return userDTO;
    }
}
